package com.app.bimo.module_accounts.mvp.ui.view;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.app.bimo.module_accounts.R;
import com.app.bimo.module_accounts.mvp.model.entiy.PreferenceData;
import com.app.bimo.module_accounts.mvp.ui.minterface.UserPreferAdapterClickListener;

import java.util.List;


public class UserPreferAdapter extends BaseAdapter {

    private Context context;
    private List<PreferenceData> list;

    private UserPreferAdapterClickListener listener;

    public void setListener(UserPreferAdapterClickListener listener) {
        this.listener = listener;
    }

    public UserPreferAdapter(Context context, List<PreferenceData> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        PreferenceData data = list.get(position);
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.account_adapter_user_prefer, null);
            holder.checkbox = convertView.findViewById(R.id.checkbox);
            holder.checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!list.get(position).isChoose()) {
                        holder.checkbox.setTextColor(context.getResources().getColor(R.color.white));
                        holder.checkbox.setBackground(context.getResources().getDrawable(R.drawable.bg_blue_38a6fe_r_5dp_btn));
                        list.get(position).setChoose(true);
                    } else {
                        holder.checkbox.setTextColor(context.getResources().getColor(R.color.g838A96));
                        holder.checkbox.setBackground(context.getResources().getDrawable(R.drawable.bg_gf2f4f7_r_5dp_btn));
                        list.get(position).setChoose(false);
                    }
                    listener.click();
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        if (data.getIslike() == 0) {
            holder.checkbox.setBackground(context.getResources().getDrawable(R.drawable.bg_gf2f4f7_r_5dp_btn));
            holder.checkbox.setTextColor(context.getResources().getColor(R.color.g838A96));
            list.get(position).setChoose(false);
            listener.click();
        } else {
            holder.checkbox.setTextColor(context.getResources().getColor(R.color.white));
            holder.checkbox.setBackground(context.getResources().getDrawable(R.drawable.bg_blue_38a6fe_r_5dp_btn));
            list.get(position).setChoose(true);
            listener.click();
        }
        holder.checkbox.setText(data.getCategoryName());
        return convertView;
    }

    class Holder {
        TextView checkbox;
    }


}
